﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Threading;
using Services;

using System.IO.IsolatedStorage;
using System.IO;
using Utilities;


namespace EnhancedTimer
{
  public class OptionsState
  {
      public bool sound;
      public bool vibration;
      public bool allowRunScreenOff;
      public int lastHours;
      public int lastMinutes;
      public int lastSeconds;
      public int alertSound;
      public OptionsState()
      {
          sound = true;
          vibration = true;
          allowRunScreenOff = true;
          lastHours = 0;
          lastMinutes = 0;
          lastSeconds = 0;
          alertSound = 0;
      }
  }
  public static class Storage
  {
    private static Func<StreamReader, OptionsState> read_options_state = reader =>
    {
        OptionsState os = new OptionsState();

        var s = reader.ReadToEnd();
        var r = s.Split(new[] { ';' });

        if (r.Length != 6)
            return os;

        os.sound = bool.Parse(r[0]);
        os.vibration = bool.Parse(r[1]);
        os.allowRunScreenOff = bool.Parse(r[2]);
        os.lastHours = Int32.Parse(r[3]);
        os.lastMinutes = Int32.Parse(r[4]);
        os.lastSeconds = Int32.Parse(r[5]);
        os.alertSound = Int32.Parse(r[6]);
        return os;
        
    };

    private static Action<StreamWriter, OptionsState> write_options_state = (writer, entry) =>
        {
            var s = "" + entry.sound + ";" + entry.vibration + ";" + entry.allowRunScreenOff + ";"
                + entry.lastHours + ";" + entry.lastMinutes + ";" + entry.lastSeconds + ";" + entry.alertSound;
            writer.Write(s);
        };


    public static void SaveOptionsState(OptionsState entry)
    {
#if WINDOWS || XBOX
        using (var writer = new StreamWriter("options.txt"))
            write_options_state(writer, entry);
#else
      using (var appStorage = IsolatedStorageFile.GetUserStoreForApplication())
      {
        appStorage.DeleteFile("options.txt");
        using (var streamFile = appStorage.OpenFile("options.txt", FileMode.OpenOrCreate))
        using (var writer = new StreamWriter(streamFile))
            write_options_state(writer, entry);
      }
#endif
    }



    public static OptionsState ResetOptionsState()
    {
        var os = new OptionsState();
        SaveOptionsState(os);
        return os;
    }

    public static OptionsState GetOptionsState()
    {
#if WINDOWS || XBOX
        try
        {
            using (var reader = new StreamReader("options.txt"))
                return read_options_state(reader);
        }
        catch (Exception)
        { return ResetOptionsState(); }
#else
      using (var appStorage = IsolatedStorageFile.GetUserStoreForApplication())
      {
        using (var streamFile = appStorage.OpenFile("options.txt", FileMode.OpenOrCreate))
        {
          try
          {
            using (var reader = new StreamReader(streamFile))
                return read_options_state(reader);
          }
          catch (Exception) { return ResetOptionsState(); }
        }
      }
#endif
    }

  }
}
